package com.cmrx.model.controller;

import com.cmrx.model.entity.AnalysisSuggestion;
import com.cmrx.model.entity.CaseDetail;
import com.cmrx.model.entity.CommissionTools;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.AnalysisService;
import com.cmrx.model.service.InvestigationService;
import com.cmrx.model.service.LoginService;
import com.cmrx.model.service.SysdictService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.LogUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.util.UUIDUtil;
import com.cmrx.model.vo.ResultVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("analysis")
@Slf4j
@Api(value = "/analysis",tags = "分析意见接口类" ,description = "分析意见接口类" )
public class AnalysisController {
	@Autowired
	private AnalysisService analysisService;
	@Autowired
	private SysdictService sysdictService;
	@Autowired
	private LoginService loginService;
    @Autowired
    private InvestigationService investigationService;


    /*@ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })*/
	@RequestMapping(value = "/detail",method = RequestMethod.POST)
    @ApiOperation(value = "分析意见详情接口",notes="分析意见详情备注说明")
    public ResultVo selectInvestigationList(@RequestBody CaseDetail caseDetail, HttpServletRequest request) {
            String token = caseDetail.getToken();
            if(StringUtils.isEmpty(token)){
                return ResultVoUtil.message("token为空");
            }
            User user = JwtUtil.deToken(token);
            if(user==null){
                return ResultVoUtil.message("用户信息为空");
            }
            String teachid = user.getTechid();
			if(StringUtils.isEmpty(caseDetail.getInvestigationid())) {
				return ResultVoUtil.message("investigationid为空");
			}
            List<CaseDetail> caseDetails =  investigationService.selectListLawCase(null, caseDetail.getInvestigationid());
            if(caseDetails!=null){
                caseDetail = caseDetails.get(0) ;
            }
			AnalysisSuggestion  analysisSuggestion = analysisService.selectAnalysisSuggestionById(caseDetail,teachid);
            return ResultVoUtil.success(analysisSuggestion);
	}
	@RequestMapping(value = "/addorupdate/tool",method = RequestMethod.POST)
    @ApiOperation(value = "分析意见添加或修改作案工具接口")
	public ResultVo addOrUpdateTool(@RequestBody CommissionTools commissionTools) {
			String token = commissionTools.getToken();
			User user = JwtUtil.deToken(token);
		    commissionTools.setCreate_user(user.getUserName());
		    commissionTools.setUpdate_user(user.getUserName());
			if(!StringUtils.isEmpty(commissionTools.getAnalysis_id())) {
				return ResultVoUtil.success(analysisService.addOrUpdateTool(commissionTools));
			}
			return ResultVoUtil.message("analysis_id为空");
	}
	@RequestMapping(value = "/delete/tool",method = RequestMethod.POST)
    @ApiOperation(value = "分析意见删除作案工具接口")
	public ResultVo deleteTool(@RequestBody CommissionTools commissionTools) {
			analysisService.deleteTool(commissionTools);
            return ResultVoUtil.success(null);
	}
	@RequestMapping(value = "/addorupdate/analysis",method = RequestMethod.POST)
    @ApiOperation(value = "分析意见修改详情接口")
	public ResultVo addorupdateAnalysis(@RequestBody AnalysisSuggestion analysisSuggestion) {
			String token = analysisSuggestion.getToken();
			User user = JwtUtil.deToken(token);
		    analysisSuggestion.setCreate_user(user.getUserName());
			analysisSuggestion.setUpdate_user(user.getUserName());
			analysisSuggestion.setAnalysed_org(user.getOrganName());
			if(StringUtils.isEmpty(analysisSuggestion.getInvestigation_id())) {
                return ResultVoUtil.message("investigation_id为空");
			}
			analysisService.addorupdateAnalysis(analysisSuggestion,user);
            return ResultVoUtil.success(null);
	}

	@RequestMapping(value = "/select/tool/name",method = RequestMethod.POST)
    @ApiOperation(value = "选择工具接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\"}")
	public ResultVo selectToolName(@RequestBody Map<String,String> map) {
		String token = map.get("token");
		User user = JwtUtil.deToken(token);
		String userCard = user.getUserCard();
		if(StringUtils.isEmpty(userCard)) {
			return ResultVoUtil.message("userCard为空");
		}
		return ResultVoUtil.success(analysisService.selectToolName(userCard));
	}

	@RequestMapping(value = "/add/tool/name",method = RequestMethod.POST)
    @ApiOperation(value = "添加工具接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\",\"name\":\"\"}")
	public ResultVo addToolName(@RequestBody Map<String,String> map) {
		String token = map.get("token");
		User user = JwtUtil.deToken(token);
		String userCard = user.getUserCard();
		Map<String,String> param = new HashMap<>();
		String name = map.get("name");
		param.put("id",UUIDUtil.getUUID());
		param.put("name",name);
		param.put("userCard",userCard);
        if(!StringUtils.isEmpty(name)){
            analysisService.addToolName(param);
        }
		return ResultVoUtil.success(param);
	}

	@RequestMapping(value = "/delete/tool/name",method = RequestMethod.POST)
    @ApiOperation(value = "删除工具接口")
    @ApiImplicitParam(name = "map",value = "{\"token\":\"\",\"id\":\"\"}")
	public ResultVo deletetToolName(@RequestBody Map<String,String> map) {
		String id = map.get("id");
		analysisService.deleteToolName(id);
		return ResultVoUtil.success(null);
	}


	

}
